package com.pfizer.XmlContentHandler;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.pfizer.Entity.Question;
import com.pfizer.Entity.QuestionOption;
import static com.pfizer.Entity.Question.*;

/**
 * QuestionContentHandler--<b>解析问题XML數據</b> 本類用於完成解析XML信息，將解析后的XML放於list中供外部訪問
 * 
 * @author krui
 * @version 1.0 2011/11/21
 */
public class QuestionContentHandler extends DefaultHandler {
	private List<Question> questions;
	private QuestionOption qOption;
	private Question question;
	private StringBuilder sb = new StringBuilder();

	/**
	 * 解析XML數據放于List中，此方法用於外部獲得解析后的數據
	 * 
	 * @return List<Question>
	 */

	public List<Question> getQuestions() {
		return questions;
	}

	/**
	 * 在根標籤開始時就實例化list用於存放每組數據
	 */
	@Override
	public void startDocument() throws SAXException {
		questions = new ArrayList<Question>();
	}

	/**
	 * 開始解析數據，將得到的數據放於javabean
	 * 
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		if(ch[0]=='\n'&&length==1)
		{
			return;
		}
		String sTmp = new String(ch, start, length);
		sTmp=sTmp.trim();
		if(sTmp.length()==0)
		{
			return;
		}
		// (2)不管在startElement到endElement的过程中，执行了多少次characters，
		// 都会将内容添加到StringBuilder中，不会丢失内容
		sb.append(sTmp);
	}

	/**
	 * 在一個tag開始時實例化組對象，
	 */
	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		if (QUESTIONSTRING.equals(localName)) {
			question = new Question();
		} else if (OPTIONSTRING.equals(localName)) {
			qOption = new QuestionOption(attributes.getValue("ID"), attributes
					.getValue("Is_Correct"));
		}
	}

	/**
	 * 在一個tag結束后將本組數據放於list
	 */
	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		if (OPTIONSTRING.equals(localName) && qOption != null) {
			String valueString = sb.toString();
			qOption.setDesc(valueString);
			question.setOption(qOption);
			qOption = null;
		} else if (QUESTIONSTRING.equals(localName) && question != null) {
			question.setOptionString();
			questions.add(question);
			question = null;
		} else {
			if (question != null) {
				String valueString = sb.toString();
				if (QUESTIONIDSTRING.equals(localName)) {
					question.setID(valueString);
				} else if (QUESTIONNOSTRING.equals(localName)) {
					question.setNo(valueString);
				} else if (QUESTIONSUBJSTRING.equals(localName)) {
					question.setSubj(valueString);
				} else if (ANSWERTYPESTRING.equals(localName)) {
					question.setAnswerType(valueString);
				} else if (OPTIONCNTSTRING.equals(localName)) {
					question.setOptionCnt(valueString);
				} else if (MINSELECTCNTSTRING.equals(localName)) {
					question.setMinSelectCnt(valueString);
				} else if (MAXSELECTCNTSTRING.equals(localName)) {
					question.setMaxSelectCnt(valueString);
				} 
			}
		}
		sb.delete( 0, sb.length());
	}
}
